import { Access } from 'utils/access.js';
App({
  onLaunch: function (options) {
    // 查看是否授权
    let that=this;
    if (options.scene == 1001){
      wx.setStorageSync('topid', options.query.top_id)
    }
    if (options.query.state==3){
      wx.switchTab({
        url: '/' + options.path
      })
      this.globalData.state=3
    }
    wx.setStorageSync('topid', options.query.top_id)
    if (options.query.top_id){
      wx.getSetting({
        success: function (res) {
          if (res.authSetting['scope.userInfo']) {
            var members = wx.getStorageSync('members');
            if (!members) {
              var access = new Access();
              wx.getUserInfo({
                success: function (rs) {
                  var userinfo = rs.userInfo;
                  access.updateUserInfo(userinfo);
                }
              })
            }
          } else {
            wx.navigateTo({
              url: '/pages/auth/auth'
            })
          }
        }
      })
    }

    // setInterval(function(){
      // that.delrocrd();
    // },3000)
    // var that = this;
    // if (options.query.scene) {
    //   let scene = decodeURIComponent(options.query.scene);
    //   that.globalData.su_id = scene
    // }

    wx.getSetting({          //第一先判断有没有授权 
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
        } else {             
          // wx.redirectTo({       //否则跳转登录
          //   url: '/pages/auth/auth'
          // })
        }
      }
    })
    var members = wx.getStorageSync('members');
    if (!members.openid) {
      var access = new Access();
      wx.getUserInfo({
        lang: 'zh_CN',
        success: function (rs) {
          var userinfo = rs.userInfo;
          access.updateUserInfo(userinfo);
        }
      })
    }
  },
  delrocrd() {
    var that = this;
    wx.authorize({
      scope: 'scope.userLocation',
      success(res) {
        wx.getLocation({
          type: 'wgs84',
          altitude: true,//高精度定位
          success: function (resdata) {
            let obj = {
              jingdu: resdata.latitude,
              weidu: resdata.longitude
            }
            wx.setStorage({
              key: 'meny',
              data: obj,
            })
           
          },
        })
      },
      fail() {
        wx.showModal({
          title: '提示',
          content: '您未授权地理位置，功能将无法使用',
          showCancel: true,
          confirmText: "授权",
          confirmColor: "#52a2d8",
          success: function (res) {
            if (res.confirm) {
              //确认则打开设置页面（重点）
              wx.openSetting({
                success: (res) => {
                  if (!res.authSetting['scope.userLocation']) {
                    //未设置录音授权
                    wx.showModal({
                      title: '提示',
                      content: '您未授权地理位置，功能将无法使用',
                      showCancel: false,
                      success: function (res) {
                      },
                    })
                  } else {
                    //第二次才成功授权
                    wx.getLocation({
                      type: 'wgs84',
                      altitude: true,//高精度定位
                      success: function (resdata) {
                        let obj = {
                          jingdu: resdata.latitude,
                          weidu: resdata.longitude
                        }
                        wx.setStorage({
                          key: 'meny',
                          data: obj,
                        })
                      },
                    })
                  }
                },
                fail: function () {
                  
                }
              })
            } else if (res.cancel) {
              
            }
          },
          fail: function () {
            
          }
        })
      }
    })

  },
  onShow:function(){
    
  },
  //验证手机号码
  validatePhone: function (phoneNum) {
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(phoneNum)) {
      return false;
    } else {
      return true;
    }
  },
  globalData: {
    su_id: '',
    state:''
    // type:null,
    // activeindex:''
  }
})